﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using EvaluationSystem.DAO;
using System.Data;
using EvaluationSystem.Exceptions;

namespace EvaluationSystem.ConfigurationPages.EvaluationItemsPages
{
    public partial class EvaluationItemsAddingPage : System.Web.UI.Page
    {
        SummaryManager summaryManager = new SummaryManager();
        EvaluationItemsManager evaluationManager = new EvaluationItemsManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Utility.IsLogin(this.Page))
            {
#if !DEBUG
                Response.Redirect("../Default.aspx");
#endif
            }
            if (!X.IsAjaxRequest)
            {
                InitEvaluationItems();
            }
        }

        protected void InitEvaluationItems()
        {
            DataTable dt = summaryManager.GetAllSummary();
            foreach (DataRow row in dt.Rows)
            {
                cbbSummary.Items.Add(new Ext.Net.ListItem((string)row["SummaryName"], row["Id"].ToString()));
            }
            if (cbbSummary.Items.Count > 0)
            {
                cbbSummary.SelectedIndex = 0;
            }
        }

        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            try
            {
                bool isSuccess = evaluationManager.AddEvaluationItem(cbbSummary.SelectedItem.Value, txtEvalutionItemName.Text.Trim());
                if (isSuccess)
                {
                    X.MessageBox.Notify(Constants.Noticfy, "增加评分指标内容成功.").Show();
                }
                else
                {
                    X.MessageBox.Alert(Constants.Noticfy, "保存出错，请重新尝试或联系开发人员.").Show();
                }
            }
            catch (ExistEvaluationException ex)
            {
                X.MessageBox.Alert(Constants.Noticfy, ex.Message).Show();
                txtEvalutionItemName.Focus();
                txtEvalutionItemName.SelectText(0);
            }
        }
    }
}